package com.yunjian.core.api;


import java.util.List;
import java.util.Map;

import com.yunjian.common.utils.LanguageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunjian.common.utils.Constant;
import com.yunjian.common.utils.PageUtils;
import com.yunjian.common.utils.StringUtil;
import com.yunjian.core.dto.ResponseDto;
import com.yunjian.core.entity.PhoneBrand;
import com.yunjian.core.entity.PhoneModel;
import com.yunjian.core.service.IPhoneBrandService;
import com.yunjian.core.service.IPhoneModelService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiapengyu
 * @since 2020-02-19
 */
@RestController
@RequestMapping("/api/phoneBrand")
public class PhoneBrandController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IPhoneBrandService phoneBrandService;

    @Autowired
    private IPhoneModelService phoneModelService;

    /**
     *查询品牌列表
     * @return
     */
    @RequestMapping(value="/queryBrandList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto queryBrandList(@RequestBody Map<String, Object> param){
        logger.info("查询品牌列表");
        ResponseDto response = new ResponseDto(Constant.SUCCESS_CODE, null, Constant.SUCCESS_MESSAGE);
        List<PhoneBrand> list = phoneBrandService.list(new QueryWrapper<PhoneBrand>() .eq("delete_flag", 1));
        response.setData(list);
        return response;
    }

    /**
     *分页查询品牌列表
     * @return
     */
    @RequestMapping(value="/queryBrandListByPage", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto queryBrandListByPage(@RequestBody Map<String, Object> param){
        logger.info("分页查询品牌列表{}", param);
        ResponseDto response = new ResponseDto(Constant.SUCCESS_CODE, null, Constant.SUCCESS_MESSAGE);
        try {
            PageUtils page = phoneBrandService.queryPage(param);
            response.setData(page);
        } catch (Exception e) {
            logger.error("分页查询品牌列表失败", e);
            return new ResponseDto(Constant.FAIL_CODE, null, LanguageUtils.getValueByKey("brand.query.list.fail"));
        }
        return response;
    }

    /**
     *根据品牌查询机型
     * @return
     */
    @RequestMapping(value="/queryPhoneListByBrand", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto queryPhoneListByBrand(@RequestBody Map<String, Object> param){
        logger.info("根据品牌查询机型{}", param);
        ResponseDto response = new ResponseDto(Constant.SUCCESS_CODE, null, Constant.SUCCESS_MESSAGE);
        String brandId = StringUtil.obj2String(param.get("id"));
        String type = StringUtil.obj2String(param.get("type"));
        String phoneModel = StringUtil.obj2String(param.get("phoneModel"));
        if(StringUtils.isEmpty(brandId)){
            return new ResponseDto(Constant.PARMS_ERROR_CODE, null, LanguageUtils.getValueByKey("system.param.error"));
        }
        if(StringUtils.isEmpty(type)){
            return new ResponseDto(Constant.PARMS_ERROR_CODE, null, LanguageUtils.getValueByKey("system.param.error"));
        }
        QueryWrapper query = new QueryWrapper<PhoneModel>()
                .eq("brand_id", Integer.parseInt(brandId))
                .eq("type", Integer.parseInt(type));

        if(!StringUtils.isEmpty(phoneModel)){
            query.like("phone_model", phoneModel);
        }
        query.orderByAsc("sort_num");
        List<PhoneModel> list = phoneModelService.list(query);
        response.setData(list);
        return response;
    }

    /**
     *根据品牌分页查询机型
     * @return
     */
    @RequestMapping(value="/queryPagePhoneListByBrand", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto queryPagePhoneListByBrand(@RequestBody Map<String, Object> param){
        logger.info("根据品牌分页查询机型{}", param);
        ResponseDto response = new ResponseDto(Constant.SUCCESS_CODE, null, Constant.SUCCESS_MESSAGE);
        String brandId = StringUtil.obj2String(param.get("id"));
        if(StringUtils.isEmpty(brandId)){
            return new ResponseDto(Constant.FAIL_CODE, null, LanguageUtils.getValueByKey("system.param.error"));
        }
        try {
            PageUtils page = phoneModelService.queryAppPage(param);
            response.setData(page);
        } catch (Exception e) {
            logger.error("根据品牌分页查询机型失败", e);
            return new ResponseDto(Constant.FAIL_CODE, null, LanguageUtils.getValueByKey("mobile.query.list.fail"));
        }
        return response;
    }
}
